package com.yyj.web.controller;

import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.constant.WebConstant;
import com.yyj.model.TPropertyManager;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.AdviceService;
import com.yyj.service.ManagerService;
import com.yyj.util.RspUtils;
import com.yyj.web.protocol.PageRequest;
import com.yyj.web.protocol.req.AdviceAddReq;
import com.yyj.web.protocol.req.AdviceQueryReq;
import com.yyj.web.protocol.req.AdviceReplyReq;
import com.yyj.web.protocol.req.RepairRecordQueryReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Api(value = "投诉建议相关接口" , description="投诉建议相关接口")
@RestController
public class AdviceController extends BaseController {

    @Autowired
    AdviceService adviceService;

    @ApiOperation(value = "用户提交投诉建议")
    @RequestMapping(value = "/auth/advices", method = RequestMethod.POST)
    public BaseResponse submitAdvice( @RequestBody @Validated AdviceAddReq adviceAddReq
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            adviceAddReq.setRoomNum(sessionInfo.getRoomNum());
            adviceAddReq.setMobile(sessionInfo.getMobile());
            adviceService.addAdvice(adviceAddReq);
            return RspUtils.success("提交成功");
        }catch(Exception e){
            logger.error("提交投诉异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主查询投诉建议记录")
    @RequestMapping(value = "/auth/advices" , method = RequestMethod.GET)
    public BaseResponse queryAdviceRecord(PageRequest pageRequest){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            AdviceQueryReq adviceQueryReq = new AdviceQueryReq();
            adviceQueryReq.setCurrentPage(pageRequest.getCurrentPage());
            adviceQueryReq.setPageSize(pageRequest.getPageSize());
            adviceQueryReq.setMobile(sessionInfo.getMobile());
            adviceQueryReq.setRoomNum(sessionInfo.getRoomNum());
            return RspUtils.success(adviceService.queryAdvice(adviceQueryReq));

        }catch(Exception e){
            logger.error("业主查询投诉建议记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }

    @ApiOperation(value = "业主查询投诉建议记录详情")
    @RequestMapping(value = "/auth/advices/{id}" , method = RequestMethod.GET)
    public BaseResponse queryAdviceRecord(@PathVariable Long id){
        try{
            return RspUtils.success(adviceService.queryAdviceById(id));
        }catch(Exception e){
            logger.error("业主查询投诉建议详情异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }

    @ApiOperation(value = "业主回复投诉建议")
    @RequestMapping(value = "/auth/owner/advices/reply" , method = RequestMethod.POST)
    public BaseResponse replyAdvice(@RequestBody @Validated AdviceReplyReq adviceReplyReq  , BindingResult result){
        try{
            checkRequestParams(result);
            return RspUtils.success(adviceService.ownerReply(adviceReplyReq));

        }catch(Exception e){
            logger.error("业主回复投诉建议异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }
}
